package demo01;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class mysqlConnect {

    static Connection conn = null;
    static Statement statement = null;

    public static void addUSer() {

        try {

            //1.加载数据库驱动
            Class.forName("com.mysql.cj.jdbc.Driver");

            //2.获取数据库连接(两地铁轨)
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/homework4_18",
                    "root",
                    "1234"
            );

            //3.创建操作语句的Statement对象(两地火车)
            statement = conn.createStatement();

            //4.执行sql语句
            //4.1先定义要执行的语句的代码
            String sql = "insert into employee values('E09','jack',22,'宁波',6000)";

            //4.2通过Statement对象执行sql语句
            statement.execute(sql);

        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (conn != null) {
                    conn.close();
                }
                if (statement != null) {
                    statement.close();
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public static void main(String[] args) {
        addUSer();
    }

}

